/**
 * Axios封装
 * @author shiloh
 * 2022/1/22 17:45
 */
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'

// 创建axios实例
const request: AxiosInstance = axios.create({
  baseURL: 'https://jsonplaceholder.typicode.com/'
})

// 请求拦截器配置
request.interceptors.request.use(
  (config: AxiosRequestConfig): AxiosRequestConfig => {
    // todo 统一设置Token到请求头
    if (config.headers) {
      config.headers['token'] = 'test token'
    }
    return config
  },
  (error: any) => Promise.reject(error)
)

// 响应拦截器配置
request.interceptors.response.use(
  (response: AxiosResponse): AxiosResponse => {
    // todo 检查token是否失效
    return response
  },
  (error: any) => Promise.reject(error)
)

export default request
